package com.elvin.platform.socketIO;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@Component
public class SockectIOConfig  {

  @Bean
  public SocketIOServer socketIOServer() {
    com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
    String os = System.getProperty("os.name");
    System.out.println("环境："+os);
    if(os.toLowerCase().startsWith("win")){   //在本地window环境测试时用localhost
      System.out.println("this is  windows");
      config.setHostname("localhost");
    } else {
      config.setHostname("81.68.121.222");   //部署到你的远程服务器正式发布环境时用服务器公网ip
      //config.setHostname("localhost");   //部署到你的远程服务器正式发布环境时用服务器公网ip
    }
    config.setHostname("81.68.121.222");
    config.setPort(7001);
		/*config.setAuthorizationListener(new AuthorizationListener() {//类似过滤器
			@Override
			public boolean isAuthorized(HandshakeData data) {
				//http://localhost:8081?username=test&password=test
				//例如果使用上面的链接进行connect，可以使用如下代码获取用户密码信息，本文不做身份验证
				// String username = data.getSingleUrlParam("username");
				// String password = data.getSingleUrlParam("password");
				return true;
			}
		});*/
    final SocketIOServer server = new SocketIOServer(config);
    System.out.println("socketServer:"+server);
    return server;
  }


  @Bean
  public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
    return new SpringAnnotationScanner(socketServer);
  }


}
